package com.hjq.www.View;

import com.hjq.www.Dao.ZoosDao;
import com.hjq.www.Dao.Impl.ZoosDaoImpl;
import com.hjq.www.utils.JDBCUtils;

import javax.swing.*;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;
import java.util.Vector;

/**
 * Create with IntelliJ IDEA
 *
 * @author: hjq
 * @Date:
 * @Description:
 */
public class ZoosMasterView extends JFrame {
    public static void main(String[] args) {
     new ZoosMasterView().showZoos();
    }
    ZoosDao z=new ZoosDaoImpl();
    Connection conn=null;
    PreparedStatement ps=null;
    ResultSet rs=null;

    JLabel idLabel = new JLabel("序    号: ");
    JTextField idField = new JTextField(15);
    JLabel nameLabel = new JLabel("名    称: ");
    JTextField nameField = new JTextField(15);
    JLabel typeLabel = new JLabel("展区类型: ");
    JTextField typeField = new JTextField(15);
    JLabel introductionLabel = new JLabel("简    介: ");
    JTextField introductionField = new JTextField(15);
    JLabel areaLabel = new JLabel("占地面积: ");
    JTextField areaField = new JTextField(15);
    JLabel addressLabel = new JLabel("地    址: ");
    JTextField addressField = new JTextField(15);
    JLabel startTimeLabel = new JLabel("开园时间: ");
    JTextField startTimeField = new JTextField(15);
    JLabel endTimeLabel = new JLabel("闭园时间: ");
    JTextField endTimeField = new JTextField(15);
    JLabel MasterIdLabel = new JLabel("管理员id: ");
    JTextField MasterIdField = new JTextField(15);
    ZoosDao br=new ZoosDaoImpl();
    JButton addButton = new JButton("添加");
    JButton updateButton = new JButton("修改");
    JButton searchButton=new JButton("查询");
    JButton deleteButton = new JButton("删除");
    JButton resetButton = new JButton("重置");
    public ZoosMasterView() {
        JFrame frame = new JFrame();
        DefaultTableModel dtm = new DefaultTableModel();
        DefaultTableCellRenderer dtr = new DefaultTableCellRenderer();
        dtr.setHorizontalAlignment(JLabel.CENTER);
        JTable table = new JTable(stuTableModel(dtm));
        table.setDefaultRenderer(Object.class, dtr);
        JScrollPane jsp = new JScrollPane();
        jsp.setViewportView(table);
        frame.add(jsp);
        frame.setBounds(1, 10, 1500, 170);
        JPanel panel = new JPanel();
        frame.add(panel, BorderLayout.NORTH);
        JPanel Jpanel = new JPanel();
        JButton backButton=new JButton("返回");
        backButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new MainView().init();
                dispose();
            }
        });
        Jpanel.add(backButton);
        addButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                z.add(idField,nameField,typeField,introductionField,addressField,areaField,startTimeField,endTimeField,MasterIdField);
            }
        });
        deleteButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                z.delete(idField);

            }
        });
        updateButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                z.update(idField,nameField,typeField,introductionField,addressField,areaField,startTimeField,endTimeField,MasterIdField);
            }
        });
        searchButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                z.search(idField,nameField,typeField,introductionField,addressField,areaField,startTimeField,endTimeField,MasterIdField);
            }
        });
        resetButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                z.reset(idField,nameField,typeField,introductionField,addressField,areaField,startTimeField,endTimeField,MasterIdField);
            }
        });
        JButton freshButton = new JButton("刷新");
        freshButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new ZoosMasterView();
            }
        });
        Jpanel.add(freshButton);
        frame.setTitle("园区信息表");
        frame.add(Jpanel, BorderLayout.SOUTH);
        frame.setVisible(true);

    }
    public void showZoos(){
        Container container = getContentPane();
        setBounds(520, 180, 500, 650);
        //添加一块桌布
        container.setLayout(new BorderLayout());
        JPanel root = new JPanel();
        root.setLayout(null);
        Font f = new Font("宋体", Font.PLAIN, 20);
        idLabel.setBounds(17, 1, 130, 60);
        idField.setBounds(133, 17, 280, 30);
        nameLabel.setBounds(17,50,130,60);
        nameField.setBounds(133,67,280,30);
        typeLabel.setBounds(17, 100, 130, 60);
        typeField.setBounds(133, 117, 280, 30);
        introductionLabel.setBounds(17, 150, 130, 60);
        introductionField.setBounds(133, 167, 280, 30);
        addressLabel.setBounds(17, 200, 150, 60);
        addressField.setBounds(133, 217, 280, 30);
        areaLabel.setBounds(17, 250, 130, 60);
        areaField.setBounds(133, 267, 280, 30);
        startTimeLabel.setBounds(17, 300, 130, 60);
        startTimeField.setBounds(133, 317, 280, 30);
        endTimeLabel.setBounds(17,350,130,60);
        endTimeField.setBounds(133,367,280,30);
        MasterIdLabel.setBounds(17,400,130,60);
        MasterIdField.setBounds(133,417,280,30);
        idLabel.setFont(f);
        nameLabel.setFont(f);
        typeLabel.setFont(f);
        introductionLabel.setFont(f);
        areaLabel.setFont(f);
        addressLabel.setFont(f);
        startTimeLabel.setFont(f);
        endTimeLabel.setFont(f);
        MasterIdLabel.setFont(f);
        root.add(idLabel);
        root.add(idField);
        root.add(nameLabel);
        root.add(nameField);
        root.add(typeLabel);
        root.add(typeField);
        root.add(introductionLabel);
        root.add(introductionField);
        root.add(areaLabel);
        root.add(areaField);
        root.add(addressLabel);
        root.add(addressField);
        root.add(startTimeLabel);
        root.add(startTimeField);
        root.add(endTimeLabel);
        root.add(endTimeField);
        root.add(MasterIdLabel);
        root.add(MasterIdField);
        addButton.setBounds(30, 480, 80, 50);
        updateButton.setBounds(140, 480, 80, 50);
        searchButton.setBounds(250, 480, 80, 50);
        deleteButton.setBounds(360, 480, 80, 50);
        resetButton.setBounds(200,550,80,50);
        setTitle("园区信息操作");
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        root.add(addButton);
        root.add(updateButton);
        root.add(searchButton);
        root.add(deleteButton);
        root.add(resetButton);
        container.add(root);
        this.setVisible(true);
    }
    /**
     * 使用DefaultTableModel导入表中数据
     * @param dtm
     * @return
     */
    private DefaultTableModel stuTableModel(DefaultTableModel dtm) {
        Statement st = null;
        ResultSet rs = null;
        String[] stu = {"序号","名称", "展区类型", "简介", "占地面积(km^2)", "地址", "开园时间","闭园时间","园区管理员id"};
        dtm.setColumnIdentifiers(stu);
        Vector v = null;

        try {
            conn = JDBCUtils.getConnection();
            st = conn.createStatement();
            String sql = "select * from zoopark";
            rs = st.executeQuery(sql);
            while (rs.next()) {
                v = new Vector();
                int sno = rs.getInt("id");
                String name = rs.getString("name");
                String type = rs.getString("type");
                String introduction = rs.getString("introduction");
                String address = rs.getString("address");
                String area = rs.getString("area");
                String startTime = rs.getString("startTime");
                String endTime=rs.getString("endTime");
                String bid=rs.getString("bid");
                v.add(sno);
                v.add(name);
                v.add(type);
                v.add(introduction);
                v.add(address);
                v.add(area);
                v.add(startTime);
                v.add(endTime);
                v.add(bid);
                dtm.addRow(v);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally{
            try{
                if(conn!=null){
                    conn.close();
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        return dtm;
    }

}
